

#include <stdio.h>
#include <string.h>

#include "iotv30_config.h"
#include "rtc_calendar.h"
//#include "cm_fs.h"  

#include "user_flash.h"

#include "user_nv.h"




static uint8_t u_user_config_save_flag = 0;
static uint8_t u_coord_timezone_save_flag = 0;

//其他参数可存储在一起节约空间
user_config_t user_config = {

    .ip = {"110.41.4.96"},     //"139.9.25.211"
    .port = 10096,

    .longitude = 113250000, //113.25*1000000
    .latitude = 23250000, 
    .time_zone = 8,

    .maintenance_mode = SYS_MODE_PATROL,

};



/**
 * @brief 
 * 
 */
void coord_timezone_save(int32_t longitude,int32_t latitude, int8_t time_zone)
{
    user_config.longitude = longitude;
    user_config.latitude  = latitude;
    user_config.time_zone = time_zone;

    u_coord_timezone_save_flag = 1;
}



/**
 * @brief 
 * 
 */
void coord_timezone_save_enable(void)
{
    u_coord_timezone_save_flag = 1;
}



//user_config_save_enable
void user_config_save_enable(void)
{
    u_user_config_save_flag = 1;
}




/**
 * @brief 
 * 
 * @param 
* @ret 1=ok ,0=fail
 */    

uint8_t coord_timezone_check(int32_t longitude,int32_t latitude, int8_t time_zone)
{
    if(longitude > 180000000 || longitude < -180000000)
    {
        return 0;
    }
    
    if(latitude > 90000000 || latitude < -90000000)
    {
        return 0;
    }

    if(time_zone > 12 || time_zone < -12)
    {
        return 0;
    }
    return 1;
}

/**
 * @brief 根据全局变量timezone设置时区
 * 
 */
//void timezone_set(int8_t time_zone )
void timezone_set(void )
{
    if(RTC_get_timezone() != user_config.time_zone )
    {
        //user_config.time_zone = time_zone;
        RTC_set_timezone(user_config.time_zone);
    }
}






/**
 * @brief 保存服务器地址
 * 
 * @param ip 
 * @param port 
 */
void server_addr_save(char* ip, uint16_t port)
{
    strcpy(user_config.ip, ip);
    user_config.port = port;
    user_config_save_enable( );
}



static void user_config_default(void)
{				
    memset((char*)&user_config.First,0, sizeof(user_config_t)  );
    user_config.First = FLASH_USER_CONFIG_FIRST_WR ;	


//#define    defualt_ip_str_ktj      "110.41.4.96"
//#define    defualt_port_str_ktj    "10096"  


    // user_config = {

    //     .ip = {"110.41.4.96"},     //  "139.9.25.211"
    //     .port = 10096,

    //     .longitude = 113250000, //113.25*1000000
    //     .latitude = 23250000, 
    //     .time_zone = 8,

    //     .maintenance_mode = SYS_MODE_PATROL,

    // };


        strncpy(user_config.ip,defualt_ip_str_ktj, IP_STRING_LEN );       // "110.41.4.96"}   "139.9.25.211"
        user_config.port = 10096;

        user_config.longitude = 113250000; //113.25*1000000
        user_config.latitude = 23250000; 
        user_config.time_zone = 8;

        user_config.maintenance_mode = SYS_MODE_PATROL;


	 user_config_save_enable( ); //开机即保存，防止看门狗复位	

}





/**
 * @brief user config init
 * 
 */
 void user_config_init(void)
{

  FILE * pFile = NULL;

  logout( "UserFlash_Init()\n");

    memset((uint8_t*)&user_config.First,0, sizeof(user_config_t)  );

     pFile = File_Unlock( USER_CONFIG_FILE_NAME ) ;

  //文件操作
	if (  NULL == pFile)
	{// //打开失败---文件不存在
		logout( "%s fail! open file %s fail!\n",__FUNCTION__,USER_CONFIG_FILE_NAME );

	}else{

        ReadDataFromFlash_8Bits(pFile, (uint8_t*)&user_config.First,FLASH_USER_CONFIG_PAGE,sizeof(user_config_t) );  

        if( FLASH_USER_CONFIG_FIRST_WR != user_config.First ) 
        {//第一次上电
            user_config_default( );           
        }

       File_Lock( pFile);
	}		

}



void User_Config_SaveProcess(void)
{

  FILE *  pFile = NULL;

   if( u_coord_timezone_save_flag )
    {
        u_coord_timezone_save_flag = 0;
        timezone_set();       
    }


	if( 0==u_user_config_save_flag )
	{		
        return ;
	}

	 u_user_config_save_flag = 0;


		 //打开文件
		//pFile = fopen (USER_FLASH_FILE_PATH,"r+b");//以（读写）形式打开文件    USER_FLASH_FILE  
         pFile = File_Unlock( USER_CONFIG_FILE_NAME ) ;


	   //文件操作
		if (  NULL != pFile)
		{//
            WriteDataToFlash_8Bits(pFile, (uint8_t*)&user_config.First,FLASH_USER_CONFIG_PAGE,sizeof(user_config_t) );  	

            File_Lock(pFile); 

			//fclose(pFile); //注：fclose在关闭文件的时候，也会刷新缓冲区	
			//pFile = NULL;

		}else{ //打开失败---文件不存在

            logout( "save user_config flash fail!\n" );
		}

}

